package com.koron.standard.jobStock.maintain.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koron.standard.jobStock.maintain.bean.dto.MaintainItemDTO;
import com.koron.standard.jobStock.maintain.bean.po.MaintainItem;
import com.koron.standard.jobStock.maintain.bean.po.MaintainItemParams;
import com.koron.standard.jobStock.maintain.mapper.MaintainItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 日常维护标准下工作项目(MaintainItem)表服务实现类
 *
 * @author jzp
 * @since 2021-09-07
 */
@Service
public class MaintainItemService extends ServiceImpl<MaintainItemMapper, MaintainItem> {

    @Autowired
    private MaintainItemParamsService maintainItemParamsService;

    public List<MaintainItemDTO> getDtoByMaintainId(String maintainId) {
        return getBaseMapper().selectDtoListByMaintainId(maintainId);
    }

    public List<MaintainItemDTO> getTreeByMaintainId(String maintainId) {
        return toTree(getDtoByMaintainId(maintainId));
    }

    /**
     * DTO > tree
     */
    public List<MaintainItemDTO> toTree(List<MaintainItemDTO> itemDtos) {
        List<MaintainItemDTO> itemDtoRoots = itemDtos.stream().filter(dto -> "0".equals(dto.getParentId())).collect(Collectors.toList());
        for (MaintainItemDTO itemDtoRoot : itemDtoRoots) {
            List<MaintainItemDTO> children = new ArrayList<>();
            for (int i = 0; i < itemDtos.size(); i++) {
                if (itemDtoRoot.getId().equals(itemDtos.get(i).getParentId())) {
                    children.add(itemDtos.get(i));
                    itemDtos.remove(i);
                    i = i - 1;
                }
            }
            itemDtoRoot.setChildren(children);
        }
        return itemDtoRoots;
    }

    public void saveRootDto(MaintainItemDTO maintainItemDTO) {
        saveDto(maintainItemDTO, "0");
    }

    public void saveDto(MaintainItemDTO maintainItemDTO, String parentId) {

        // 1. 保存工作项目
        maintainItemDTO.setParentId(parentId);
        save(maintainItemDTO);

        String maintainId = maintainItemDTO.getMaintainId();
        String maintainItemId = maintainItemDTO.getId();

        // 2. 保存记录项
        List<MaintainItemParams> maintainItemParams = maintainItemDTO.getItemParams();
        // 设置作业标准id和作业标准项id
        for (MaintainItemParams maintainItemParam : maintainItemParams) {
            maintainItemParam.setMaintainId(maintainId);
            maintainItemParam.setItemId(maintainItemId);
        }
        maintainItemParamsService.saveBatch(maintainItemParams);
        List<MaintainItemDTO> maintainItemDTOS = Optional.ofNullable(maintainItemDTO.getChildren()).orElse(new ArrayList<>());

        // 3. 保存子节点工作项目
        for (MaintainItemDTO child : maintainItemDTOS) {
            child.setMaintainId(maintainId);
            saveDto(child, maintainItemId);
        }

    }

    public void removeByMaintainId(String maintainId) {
        if (StrUtil.isEmpty(maintainId)) {
            return;
        }
        LambdaQueryWrapper<MaintainItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaintainItem::getMaintainId, maintainId);
        List<MaintainItem> list = list(wrapper);
        removeByIds(list.stream().map(MaintainItem::getId).collect(Collectors.toList()));
        maintainItemParamsService.removeByMaintainId(maintainId);
    }
}
